package com.yenlien.traceyun.webase.controller;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.yenlien.traceyun.common.crypt.TokenUtil;
import com.yenlien.traceyun.common.exception.TokenException;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wangyang@3sqy.cn
 * @version V1.0
 * @date 2017年11月15日
 * @Description: 基础controller类
 */
public class BaseController {

    /**
     * 返回值
     *
     * @param data 数据
     * @return object
     */
    public Object success(Object data) {
        JSONObject result = new JSONObject();
        result.put("code", "200");
        result.put("data", data);
        return result;
    }

    /**
     * 返回值
     *
     * @param data 数据
     * @return object
     */
    public Object fail(Object data) {
        JSONObject result = new JSONObject();
        result.put("code", "500");
        result.put("data", data);
        return result;
    }

    /**
     * 返回值
     *
     * @param page 数据
     * @return object
     */
    public <T> Object buildPage(Page<T> page) {
        JSONObject result = new JSONObject();
        result.put("code", "200");
        result.put("total", page.getTotalElements());
        result.put("rows", page.getContent());
        return result;
    }

    public <T> Object buildPage(List<T> list, int total) {
        JSONObject result = new JSONObject();
        result.put("code", "200");
        result.put("total", total);
        result.put("rows", list);
        return result;
    }


    /**
     * 返回值
     *
     * @param data           data
     * @param clazz          clazz
     * @param propertyfilter propertyfilter
     * @return object
     */
    public <T> Object success(Object data, Class<T> clazz, String[] propertyfilter) {
        SimplePropertyPreFilter simplePropertyPreFilter = new SimplePropertyPreFilter(clazz, propertyfilter);
        StringBuffer sb = new StringBuffer();
        sb.append("{").append("\"code\":\"200\",").append("").append("\"data\":");
        sb.append(JSONObject.toJSONString(data, simplePropertyPreFilter, SerializerFeature.WriteMapNullValue)).append("").append("}");
        return JSONObject.parse(sb.toString());
    }


    public String getMerchantId() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request.getSession().getAttribute("userId").toString();
    }

//	public UserBean getUserbean(){
//		HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//		return  (UserBean) request.getSession().getAttribute("user");
//	}
//

    public String getLoginUserId() throws TokenException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            throw new TokenException("未登录");
        }
        JSONObject jsonObject = JSONObject.parseObject(TokenUtil.getBody(token));
        return jsonObject.getString("id");
    }


}
